package com.kangye.exam.service.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kangye.exam.bean.Student;
import com.kangye.exam.dao.StudentDao;
import com.kangye.exam.service.StudentService;
import com.kangye.exam.utils.JsonUtil;
import com.kangye.exam.utils.RedisTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "studentService")
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentDao studentDao;

    @Resource
    private RedisTool redisTool;

    @Override
    public Student findSId(int studentId) {
        return studentDao.findSId(studentId);
    }

    @Override
    public PageInfo<Student> querySAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Student> students = studentDao.querySAll();
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        return studentPageInfo;
    }

    @Override
    public PageInfo<Student> studentList(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        List<Student> students = studentDao.studentList(map);
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        return studentPageInfo;
    }

    @Override
    public PageInfo<Student> findTId(Integer teacherId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("teacherId",teacherId);
        List<Student> students = studentDao.findTId(map);
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        return studentPageInfo;
    }

    @Override
    public void insertStu(Student student) {
        studentDao.insertStu(student);
    }

    @Override
    public void updateStu(Student student) {
        studentDao.updateStu(student);
    }

    @Override
    public void deleteSId(int studentId) {
        studentDao.deleteSId(studentId);
    }

    @Override
    public Student studentLogin(String studentUser, String studentPass) {
        Map<String,Object> map = new HashMap<>();
        map.put("studentUser",studentUser);
        map.put("studentPass",studentPass);
        return studentDao.studentLogin(map);
    }

    @Override
    public Map<String, Object> findStuById(Integer studentId,String sessionId) {
        Map<String, Object> map = new HashMap<>();
        map.put("studentId",studentId);
        Student student = studentDao.findStuById(map);
        if(student!=null){
            //将当前用户转换为json格式存到map中
            String stu = JsonUtil.bean2Json(student);
            Map<String,Object> stuMap = new HashMap<>();
            stuMap.put(sessionId,stu);
            //把当前用户存放到redis中,设置用户过期时间3600s
            redisTool.hmset("stu",stuMap,3600);
            map.put("code",0);
            map.put("student",student);
        }else{
            map.put("code",-1);
        }
        return map;

    }

    @Override
    public boolean stuUpd(Student student) {
        return studentDao.stuUpd(student)>0;
    }
}
